package com.powergisol.gis.dto;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.powergisol.gis.geom.GeoPoint;
import com.powergisol.gis.geom.GeoPolygon;
import com.vividsolutions.jts.geom.Coordinate;

import java.util.ArrayList;
import java.util.List;

public class ModuleDTO {

    /**
     * conbination : [[0,1,3],[3,2,0],[7,5,4],[4,6,7],[11,9,8],[8,10,11],[12,13,15],[15,14,12],[19,17,16],[16,18,19],[20,21,23],[23,22,20],[24,25,27],[27,26,24],[28,29,31],[31,30,28],[35,33,32],[32,34,35],[36,37,39],[39,38,36],[40,41,43],[43,42,40],[47,45,44],[44,46,47],[51,49,48],[48,50,51],[55,53,52],[52,54,55],[59,57,56],[56,58,59],[63,61,60],[60,62,63],[64,65,67],[67,66,64],[71,69,68],[68,70,71],[72,73,75],[75,74,72],[76,77,79],[79,78,76],[83,81,80],[80,82,83],[84,85,87],[87,86,84],[91,89,88],[88,90,91],[92,93,95],[95,94,92],[99,97,96],[96,98,99],[100,101,103],[103,102,100],[104,105,107],[107,106,104],[111,109,108],[108,110,111],[112,113,115],[115,114,112],[119,117,116],[116,118,119],[120,121,123],[123,122,120],[127,125,124],[124,126,127],[128,129,131],[131,130,128],[132,133,135],[135,134,132],[139,137,136],[136,138,139],[140,141,143],[143,142,140],[144,145,147],[147,146,144],[148,149,151],[151,150,148],[152,153,155],[155,154,152],[159,157,156],[156,158,159],[163,161,160],[160,162,163],[164,165,167],[167,166,164],[171,169,168],[168,170,171],[172,173,175],[175,174,172],[179,177,176],[176,178,179],[183,181,180],[180,182,183],[184,185,187],[187,186,184],[191,189,188],[188,190,191],[192,193,195],[195,194,192],[199,197,196],[196,198,199],[203,201,200],[200,202,203],[204,205,207],[207,206,204],[208,209,211],[211,210,208],[215,213,212],[212,214,215],[219,217,216],[216,218,219],[220,221,223],[223,222,220],[227,225,224],[224,226,227],[228,229,231],[231,230,228],[232,233,235],[235,234,232],[236,237,239],[239,238,236],[240,241,243],[243,242,240],[247,245,244],[244,246,247],[251,249,248],[248,250,251],[255,253,252],[252,254,255],[259,257,256],[256,258,259],[260,261,263],[263,262,260],[264,265,267],[267,266,264],[271,269,268],[268,270,271],[275,273,272],[272,274,275],[276,277,279],[279,278,276],[280,281,283],[283,282,280],[287,285,284],[284,286,287],[288,289,291],[291,290,288],[295,293,292],[292,294,295],[299,297,296],[296,298,299],[303,301,300],[300,302,303],[307,305,304],[304,306,307],[311,309,308],[308,310,311],[312,313,315],[315,314,312],[319,317,316],[316,318,319],[320,321,323],[323,322,320],[324,325,327],[327,326,324],[331,329,328],[328,330,331],[332,333,335],[335,334,332],[336,337,339],[339,338,336],[343,341,340],[340,342,343],[344,345,347],[347,346,344]]
     * level : -6
     * points : [[113.227837,23.18766,9.55913448],[113.227852,23.1876373,9.759204],[113.227837,23.18766,7.2422514],[113.227852,23.1876373,7.35704231],[113.227859,23.1876144,9.785396],[113.227852,23.1876373,9.759204],[113.227859,23.1876144,7.36817837],[113.227852,23.1876373,7.35704231],[113.227837,23.18766,9.55913448],[113.227821,23.187685,9.239117],[113.227837,23.18766,7.2422514],[113.227821,23.187685,7.06405163],[113.22789,23.1875687,9.503963],[113.2279,23.1875439,9.32105],[113.22789,23.1875687,7.167808],[113.2279,23.1875439,7.03486729],[113.227875,23.1875916,9.676793],[113.227859,23.1876144,9.785396],[113.227875,23.1875916,7.29114],[113.227859,23.1876144,7.36817837],[113.227875,23.1875916,9.676793],[113.22789,23.1875687,9.503963],[113.227875,23.1875916,7.29114],[113.22789,23.1875687,7.167808],[113.227844,23.1871548,10.4907246],[113.227829,23.1871262,10.5276937],[113.227844,23.1871548,8.119799],[113.227829,23.1871262,8.141317],[113.227882,23.1873016,9.460434],[113.227875,23.1872711,9.655532],[113.227882,23.1873016,7.44358063],[113.227875,23.1872711,7.58301926],[113.227821,23.1870975,10.4005747],[113.227829,23.1871262,10.5276937],[113.227821,23.1870975,8.067913],[113.227829,23.1871262,8.141317],[113.227821,23.1870975,10.4005747],[113.227814,23.1870689,10.1531172],[113.227821,23.1870975,8.067913],[113.227814,23.1870689,7.92480946],[113.2277,23.1870327,7.99632549],[113.227684,23.1870365,7.75388145],[113.2277,23.1870327,6.66727257],[113.227684,23.1870365,6.523437],[113.2277,23.1870327,7.99632549],[113.227715,23.1870289,8.235076],[113.2277,23.1870327,6.66727257],[113.227715,23.1870289,6.80811453],[113.227432,23.1875553,8.263114],[113.227425,23.1875381,8.307491],[113.227432,23.1875553,5.53277254],[113.227425,23.1875381,5.533473],[113.227394,23.1874714,8.589724],[113.227386,23.1874542,8.64294052],[113.227394,23.1874714,5.74919844],[113.227386,23.1874542,5.78444433],[113.227386,23.1874542,8.64294052],[113.227379,23.187439,8.26211548],[113.227386,23.1874542,5.78444433],[113.227379,23.187439,5.5795393],[113.227341,23.1873531,8.04919052],[113.227341,23.187336,7.91033173],[113.227341,23.1873531,5.59422255],[113.227341,23.187336,5.56359768],[113.227341,23.1873531,8.04919052],[113.227348,23.18737,8.150161],[113.227341,23.1873531,5.59422255],[113.227348,23.18737,5.612287],[113.22789,23.18733,9.324184],[113.2279,23.1873589,9.247903],[113.22789,23.18733,7.338007],[113.2279,23.1873589,7.26911],[113.22789,23.18733,9.324184],[113.227882,23.1873016,9.460434],[113.22789,23.18733,7.338007],[113.227882,23.1873016,7.44358063],[113.227493,23.187685,8.19074],[113.227509,23.1876965,8.189325],[113.227493,23.187685,6.03902531],[113.227509,23.1876965,6.0900836],[113.227493,23.187685,8.19074],[113.227478,23.1876736,8.191191],[113.227493,23.187685,6.03902531],[113.227478,23.1876736,5.99304628],[113.22747,23.1876564,8.097786],[113.227478,23.1876736,8.191191],[113.22747,23.1876564,5.693982],[113.227478,23.1876736,5.99304628],[113.22747,23.1876564,8.097786],[113.227463,23.1876411,8.117756],[113.22747,23.1876564,5.693982],[113.227463,23.1876411,5.652585],[113.227913,23.1873875,9.144825],[113.2279,23.1873589,9.247903],[113.227913,23.1873875,6.87519073],[113.2279,23.1873589,7.26911],[113.227913,23.1873875,9.144825],[113.227921,23.1874161,9.15812],[113.227913,23.1873875,6.87519073],[113.227921,23.1874161,6.86878824],[113.227722,23.1878357,8.372951],[113.227737,23.1878471,8.415926],[113.227722,23.1878357,6.52101231],[113.227737,23.1878471,6.53638268],[113.227737,23.1878471,8.415926],[113.227753,23.1878242,8.420937],[113.227737,23.1878471,6.53638268],[113.227753,23.1878242,6.561729],[113.2278,23.18773,8.686509],[113.227791,23.1877537,8.54577],[113.2278,23.18773,6.765333],[113.227791,23.1877537,6.68315649],[113.227814,23.1877079,8.920456],[113.227821,23.187685,9.239117],[113.227814,23.1877079,6.89190626],[113.227821,23.187685,7.06405163],[113.227814,23.1877079,8.920456],[113.2278,23.18773,8.686509],[113.227814,23.1877079,6.89190626],[113.2278,23.18773,6.765333],[113.227921,23.1874981,9.021603],[113.227936,23.1874752,8.91574],[113.227921,23.1874981,6.81241226],[113.227936,23.1874752,6.731679],[113.227905,23.1875324,9.235632],[113.2279,23.1875439,9.32105],[113.227905,23.1875324,6.972038],[113.2279,23.1875439,7.03486729],[113.227905,23.1875324,9.235632],[113.227913,23.187521,9.156844],[113.227905,23.1875324,6.972038],[113.227913,23.187521,6.913675],[113.227852,23.1871986,10.210145],[113.227852,23.1871834,10.3381681],[113.227852,23.1871986,7.901972],[113.227852,23.1871834,7.99736452],[113.227844,23.18717,10.4099789],[113.227852,23.1871834,10.3381681],[113.227844,23.18717,8.072691],[113.227852,23.1871834,7.99736452],[113.227844,23.18717,10.4099789],[113.227844,23.1871548,10.4907246],[113.227844,23.18717,8.072691],[113.227844,23.1871548,8.119799],[113.227859,23.1872139,10.0642319],[113.227852,23.1871986,10.210145],[113.227859,23.1872139,7.79203033],[113.227852,23.1871986,7.901972],[113.227859,23.1872272,9.908095],[113.227859,23.1872139,10.0642319],[113.227859,23.1872272,7.672929],[113.227859,23.1872139,7.79203033],[113.227867,23.1872578,9.595903],[113.227867,23.1872425,9.749793],[113.227867,23.1872578,7.42990255],[113.227867,23.1872425,7.55055761],[113.227867,23.1872578,9.595903],[113.227875,23.1872711,9.655532],[113.227867,23.1872578,7.42990255],[113.227875,23.1872711,7.58301926],[113.227859,23.1872272,9.908095],[113.227867,23.1872425,9.749793],[113.227859,23.1872272,7.672929],[113.227867,23.1872425,7.55055761],[113.227814,23.1870689,10.1531172],[113.227806,23.1870384,9.845399],[113.227814,23.1870689,7.92480946],[113.227806,23.1870384,7.746172],[113.2278,23.1870251,9.684022],[113.227806,23.1870384,9.845399],[113.2278,23.1870251,7.6521244],[113.227806,23.1870384,7.746172],[113.2278,23.1870251,9.684022],[113.2278,23.18701,9.52238],[113.2278,23.1870251,7.6521244],[113.2278,23.18701,7.55764771],[113.227783,23.1870136,9.42305851],[113.2278,23.18701,9.52238],[113.227783,23.1870136,7.50052357],[113.2278,23.18701,7.55764771],[113.227768,23.1870155,9.299607],[113.227783,23.1870136,9.42305851],[113.227768,23.1870155,7.42943764],[113.227783,23.1870136,7.50052357],[113.22773,23.187027,8.465546],[113.227715,23.1870289,8.235076],[113.22773,23.187027,6.943466],[113.227715,23.1870289,6.80811453],[113.22773,23.187027,8.465546],[113.227737,23.1870232,8.682598],[113.22773,23.187027,6.943466],[113.227737,23.1870232,7.07050037],[113.227753,23.18702,9.234579],[113.227737,23.1870232,8.682598],[113.227753,23.18702,7.28475332],[113.227737,23.1870232,7.07050037],[113.227753,23.18702,9.234579],[113.227768,23.1870155,9.299607],[113.227753,23.18702,7.28475332],[113.227768,23.1870155,7.42943764],[113.227669,23.1870384,6.88813543],[113.227684,23.1870365,7.75388145],[113.227669,23.1870384,6.88813543],[113.227684,23.1870365,6.523437],[113.227448,23.18759,8.192578],[113.227448,23.1876068,8.164446],[113.227448,23.18759,5.5575304],[113.227448,23.1876068,5.58263874],[113.227432,23.1875553,8.263114],[113.22744,23.1875725,8.22519],[113.227432,23.1875553,5.53277254],[113.22744,23.1875725,5.54066372],[113.227448,23.18759,8.192578],[113.22744,23.1875725,8.22519],[113.227448,23.18759,5.5575304],[113.22744,23.1875725,5.54066372],[113.227371,23.1874218,8.267515],[113.227364,23.1874046,8.254266],[113.227371,23.1874218,5.60168076],[113.227364,23.1874046,5.615103],[113.227371,23.1874218,8.267515],[113.227379,23.187439,8.26211548],[113.227371,23.1874218,5.60168076],[113.227379,23.187439,5.5795393],[113.2274,23.1874886,8.536387],[113.227394,23.1874714,8.589724],[113.2274,23.1874886,5.71201563],[113.227394,23.1874714,5.74919844],[113.2274,23.1874886,8.536387],[113.227409,23.1875057,8.485431],[113.2274,23.1874886,5.71201563],[113.227409,23.1875057,5.674825],[113.227692,23.1878147,8.284729],[113.227707,23.1878242,8.328432],[113.227692,23.1878147,6.485217],[113.227707,23.1878242,6.50378275],[113.227676,23.1878033,8.244728],[113.227692,23.1878147,8.284729],[113.227676,23.1878033,6.465655],[113.227692,23.1878147,6.485217],[113.227661,23.1877937,8.211244],[113.227676,23.1878033,8.244728],[113.227661,23.1877937,6.44508553],[113.227676,23.1878033,6.465655],[113.227661,23.1877937,8.211244],[113.227638,23.1877823,8.186102],[113.227661,23.1877937,6.44508553],[113.227638,23.1877823,6.422949],[113.227592,23.18775,8.164313],[113.227577,23.1877384,8.168865],[113.227592,23.18775,6.334953],[113.227577,23.1877384,6.29443645],[113.227608,23.1877613,8.163946],[113.227592,23.18775,8.164313],[113.227608,23.1877613,6.36924553],[113.227592,23.18775,6.334953],[113.227531,23.187706,8.186336],[113.227509,23.1876965,8.189325],[113.227531,23.187706,6.14369],[113.227509,23.1876965,6.0900836],[113.227531,23.187706,8.186336],[113.227547,23.1877174,8.18149],[113.227531,23.187706,6.14369],[113.227547,23.1877174,6.19706059],[113.227562,23.1877289,8.175236],[113.227577,23.1877384,8.168865],[113.227562,23.1877289,6.247972],[113.227577,23.1877384,6.29443645],[113.227562,23.1877289,8.175236],[113.227547,23.1877174,8.18149],[113.227562,23.1877289,6.247972],[113.227547,23.1877174,6.19706059],[113.227455,23.187624,8.139769],[113.227448,23.1876068,8.164446],[113.227455,23.187624,5.614843],[113.227448,23.1876068,5.58263874],[113.227455,23.187624,8.139769],[113.227463,23.1876411,8.117756],[113.227455,23.187624,5.614843],[113.227463,23.1876411,5.652585],[113.227913,23.187521,9.156844],[113.227921,23.1874981,9.021603],[113.227913,23.187521,6.913675],[113.227921,23.1874981,6.81241226],[113.227928,23.18746,8.862184],[113.227936,23.1874752,8.91574],[113.227928,23.18746,6.69190359],[113.227936,23.1874752,6.731679],[113.227928,23.1874466,8.809886],[113.227921,23.1874313,9.167835],[113.227928,23.1874466,6.653319],[113.227921,23.1874313,7.134489],[113.227928,23.1874466,8.809886],[113.227928,23.18746,8.862184],[113.227928,23.1874466,6.653319],[113.227928,23.18746,6.69190359],[113.227921,23.1874161,9.15812],[113.227921,23.1874313,9.167835],[113.227921,23.1874161,6.86878824],[113.227921,23.1874313,7.134489],[113.227722,23.1878357,8.372951],[113.227707,23.1878242,8.328432],[113.227722,23.1878357,6.52101231],[113.227707,23.1878242,6.50378275],[113.22776,23.1878014,8.435977],[113.227753,23.1878242,8.420937],[113.22776,23.1878014,6.59166],[113.227753,23.1878242,6.561729],[113.227776,23.1877766,8.47147],[113.22776,23.1878014,8.435977],[113.227776,23.1877766,6.6296525],[113.22776,23.1878014,6.59166],[113.227776,23.1877766,8.47147],[113.227791,23.1877537,8.54577],[113.227776,23.1877766,6.6296525],[113.227791,23.1877537,6.68315649],[113.227333,23.18732,7.395589],[113.227325,23.1873035,6.26114464],[113.227333,23.18732,5.540653],[113.227325,23.1873035,6.26114464],[113.227333,23.18732,7.395589],[113.227341,23.187336,7.91033173],[113.227333,23.18732,5.540653],[113.227341,23.187336,5.56359768],[113.227409,23.1875057,8.485431],[113.227417,23.1875229,8.43907],[113.227409,23.1875057,5.674825],[113.227417,23.1875229,5.639893],[113.227425,23.1875381,8.307491],[113.227417,23.1875229,8.43907],[113.227425,23.1875381,5.533473],[113.227417,23.1875229,5.639893],[113.227348,23.18737,8.150161],[113.227356,23.1873875,8.216901],[113.227348,23.18737,5.612287],[113.227356,23.1873875,5.61891365],[113.227356,23.1873875,8.216901],[113.227364,23.1874046,8.254266],[113.227356,23.1873875,5.61891365],[113.227364,23.1874046,5.615103],[113.227623,23.18777,8.170476],[113.227608,23.1877613,8.163946],[113.227623,23.18777,6.398135],[113.227608,23.1877613,6.36924553],[113.227623,23.18777,8.170476],[113.227638,23.1877823,8.186102],[113.227623,23.18777,6.398135],[113.227638,23.1877823,6.422949]]
     */

    private int level;
    private List<List<Integer>> conbination;
    private List<List<Double>> points;

    @JsonIgnore
    private List<GeoPoint> geoPoints;


    public List<GeoPoint> getGeoPoints() {


        if(points.isEmpty()){
            return null;
        }

        if(null == geoPoints || geoPoints.isEmpty()){
            return null;
        }

        points.forEach(point ->{

            Double[] doubles = point.toArray(new Double[point.size()]);
            if(doubles.length!=3){
                throw  new RuntimeException("坐标只能且必须包含x,y,z 个点");
            }

            GeoPoint geoPoint = new GeoPoint(doubles[0],doubles[1],doubles[2]);
            geoPoints.add(geoPoint);
        });

        return geoPoints;
    }

    @JsonIgnore
    private List<GeoPolygon> polygons;


    /**
     * 获取里面的所有三角面
     * @return
     */
    @JsonIgnore
    public List<GeoPolygon> getPolygons() {

        /**
         * 获取三角形索引
         */
        List<List<Integer>> conbinations = getConbination();
        /**
         * 获取点集合
         */
        List<List<Double>> points = getPoints();

        polygons = new ArrayList<>();


        for (int j = 0; j < conbinations.size(); j++) {

            List<Integer> integers = conbinations.get(j);
            List<Coordinate> coordinates = new ArrayList<>();
            integers.forEach(num -> {
                List<Double> doubles = points.get(num);
                if (doubles.size() == 3) {
                    Coordinate coordinate = new Coordinate(doubles.get(0), doubles.get(1), doubles.get(2));
                    coordinates.add(coordinate);
                } else {
                    throw new RuntimeException("points 格式出错:" + points);
                }

            });

            Coordinate[] objects = coordinates.toArray(new Coordinate[coordinates.size()]);
            GeoPolygon polygon = new GeoPolygon(objects);
            polygons.add(polygon);
        }


        return polygons;
    }

    /**
     * 根据索引列表获取多边形
     * @param
     * @return
     */
    @JsonIgnore
    public GeoPolygon getGeoPolygon(List<Integer> indexs){

        List<Coordinate> coordinates = new ArrayList<>();
        indexs.forEach(index ->{

            List<Double> doubles = points.get(index);
            for (int i = 0; i < doubles.size(); i++) {
                Double x = doubles.get(i);
                Double y= doubles.get(i);
                Double z = doubles.get(i);
                Coordinate coordinate = new Coordinate(x,y,z);
                coordinates.add(coordinate);
            }
        });
        return  new GeoPolygon(coordinates.toArray(new Coordinate[coordinates.size()]));
    }




    public int getLevel() {
        return level;
    }

    public void setLevel(int level) {
        this.level = level;
    }

    public List<List<Integer>> getConbination() {
        return conbination;
    }

    public void setConbination(List<List<Integer>> conbination) {
        this.conbination = conbination;
    }

    public List<List<Double>> getPoints() {
        return points;
    }

    public void setPoints(List<List<Double>> points) {
        this.points = points;
    }
}
